package com.easycarebn.main.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.controller.ServerController;
import com.easycarebn.request.data.CommentItem;
import com.easycarebn.request.data.CommentsResponse;
import com.easycarebn.request.data.DoctorDetail;
import com.easycarebn.service.request.OnRequest;
import com.squareup.picasso.Picasso;
import com.timbacsi.main.EasyCareApplication;
import com.timbacsi.main.R;

public class FragmentComment extends Fragment {

    private View mView;
    private static FragmentComment fragment = null;
    private ArrayList<CommentItem> comments;
    private CommentsResponse commentsResponse;
    private LinearLayout lineComment;
    private ProgressBar loadding;
    int currentPage;
    int lastPage;
    private View loadmore;
    private Button btnLoadMore;
    private EasyCareApplication mApplication;

    public static FragmentComment newInstance(String title) {
        fragment = new FragmentComment();
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mApplication = (EasyCareApplication) activity.getApplicationContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_list_comment, null);
        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btnLoadMore = (Button) mView.findViewById(R.id.btnLoadMore);
        loadding = (ProgressBar) mView.findViewById(R.id.loadding);
        commentsResponse = mApplication.getmCommentsResponse();
        if (commentsResponse == null || commentsResponse.getItems() == null || commentsResponse.getItems().size() == 0) {
            btnLoadMore.setVisibility(View.INVISIBLE);
            return;

        }
        lineComment = (LinearLayout) mView.findViewById(R.id.lineComment);
        lineComment.removeAllViews();
        currentPage = commentsResponse.getPaging().getCurrentPage();
        lastPage = commentsResponse.getPaging().getLastPage();
        if (currentPage >= lastPage) {
            btnLoadMore.setVisibility(View.GONE);
        } else {
            btnLoadMore.setVisibility(View.VISIBLE);
        }
        btnLoadMore.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("loadComments", "loadComments");
                loadComments();
            }
        });
        loadComments(commentsResponse.getItems());

    }

    private void loadComments(ArrayList<CommentItem> comments) {

        if (comments == null) {
            return;
        }
        for (CommentItem comment : comments) {
            View viewItem = getActivity().getLayoutInflater().inflate(R.layout.item_comment, null);
            ImageView imgAvatar = (ImageView) viewItem.findViewById(R.id.img_avata);
            Picasso.with(getActivity()).load(comment.getAvatar()).resize(Utils.IMG_WIDHT, Utils.IMG_HEIGHT)
                    .placeholder(R.drawable.avata).error(R.drawable.avata).into(imgAvatar);

            TextView tvName = (TextView) viewItem.findViewById(R.id.lblName);
            TextView lblTime = (TextView) viewItem.findViewById(R.id.lblTime);
            lblTime.setText(comment.getTime());
            tvName.setText(comment.getName());
            if (comment.getName().isEmpty()) {
                tvName.setText(getResources().getString(R.string.chua_cap_nhat));
            } else {
                tvName.setText(comment.getName());
            }
            TextView tvComment = (TextView) viewItem.findViewById(R.id.lblContentcmt);
            tvComment.setText(comment.getComment());
            View ratingView = viewItem.findViewById(R.id.view_rating);
            if (comment.getRating() != null && comment.getRating().isDisplay()) {
                ratingView.setVisibility(View.VISIBLE);
                RatingBar rateCommon = (RatingBar) viewItem.findViewById(R.id.rateCamnhanchung);
                rateCommon.setRating(comment.getRating().getCommonSense());
                RatingBar rateFacilities = (RatingBar) viewItem.findViewById(R.id.rateCosovatchat);
                rateFacilities.setRating(comment.getRating().getFacilities());
                RatingBar rateWaitingTime = (RatingBar) viewItem.findViewById(R.id.rateThoigiancho);
                rateWaitingTime.setRating(comment.getRating().getWaitingTime());
            } else {
                ratingView.setVisibility(View.GONE);
            }
            lineComment.addView(viewItem);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void loadComments() {
        DoctorDetail dcDetail = FragmentDrDetailt.newInstance("").getCurrentDoctor();
        int doctorId = dcDetail.getDoctorId();
        if (doctorId == -1) {
            return;
        }
        loadding.setVisibility(View.VISIBLE);
        Utils.disableEnableControls(false, loadmore);
        ServerController.getInstance(getActivity()).requestGetDoctorComments(doctorId, currentPage + 1, Utils.numberOfRecord,
                new OnRequest<CommentsResponse>() {

                    @Override
                    public void onSuccess(CommentsResponse data) {
                        loadding.setVisibility(View.GONE);
                        Utils.disableEnableControls(true, loadmore);
                        comments = data.getItems();
                        currentPage = data.getPaging().getCurrentPage();
                        lastPage = data.getPaging().getLastPage();
                        if (currentPage >= lastPage) {
                            btnLoadMore.setVisibility(View.GONE);
                        } else {
                            btnLoadMore.setVisibility(View.VISIBLE);
                            loadComments(comments);
                        }

                    }

                    @Override
                    public void onError(int errorCode, String message) {
                        loadding.setVisibility(View.GONE);
                        comments = null;
                        Utils.disableEnableControls(true, loadmore);
                    }
                });
    }
}
